package com.mxfcode.bbs_common.table_operation.service.impl;

import com.mxfcode.bbs_common.table_operation.mapper.UtilMapper;
import com.mxfcode.bbs_common.table_operation.service.UtilService;
import com.mxfcode.bbs_common.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @类名 UtilServiceImpl
 * @描述 TODO
 * @作者 mxf
 * @创建日期 2020/11/2 20:11
 **/
@Service("utilService")
public class UtilServiceImpl implements UtilService {
    @Autowired
    UtilMapper utilMapper;

    @Override
    public Map<String, Object> getOneDataForId(String tableName, String id) {
        return utilMapper.getOneDataForId(tableName,id);
    }

    @Override
    public List<Map<String, Object>> getListData(String tableName,Map<String, Object> map) {
        return utilMapper.getListData(tableName,map);
    }

    @Override
    public Integer insertOneData(String tableName, Map<String, Object> map) {
        return utilMapper.insertOneData(tableName,map);
    }

    @Override
    public Integer insertDataForList(String tableName, List<Map<String, Object>> list) {
        List<String> keysList = Util.getMapKeysInList(list);
        return utilMapper.insertDataForList(tableName,list,keysList);
    }

    @Override
    public Integer deleteDataForIds(String tableName, String ids) {
        return utilMapper.deleteDataForIds(tableName,ids);
    }

    @Override
    public Integer updateOneData(String tableName,Map<String, Object> map) {
        String id = (String) map.get("id");
        map.remove("id");
        return utilMapper.updateOneData(tableName,id,map);
    }

    @Override
    public Boolean updateDataMap(String tableName, Map<String, Object> map) {
        Boolean is_success = true;
        String id = (String) map.get("id");
        map.remove("id");
        try {
            utilMapper.updateDataMap(tableName,map,id);
            return is_success;
        }catch (Exception e){
            is_success = false;
            return is_success;
        }
    }
}
